﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using IHISCETeim.Library;
using System.Data.Common;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Microsoft.Practices.EnterpriseLibrary.Data.Sql;
using IHISCETeim.Model;
namespace IHISCETeim.DAL
{
    public  class SAP_AgentListDAL
    {
        public static int Add(SAP_AgentListInfo obj)
        {
            Database DB = DatabaseFactory.CreateDatabase();

            DbCommand dbCommand = DB.GetStoredProcCommand("SAP_AgentList_Add", obj.CardCode, obj.CardName, obj.AliasName, obj.Address, obj.FromSap, obj.IsShow, obj.LinkeName, obj.Tel1, obj.Banks, obj.Account, obj.VatIdUnCmp);
            return Convert.ToInt32(DB.ExecuteScalar(dbCommand));
        }
        public static bool Delete(string CardCode)
        {
            Database DB = DatabaseFactory.CreateDatabase();

            DbCommand dbCommand = DB.GetStoredProcCommand("SAP_AgentList_Delete", CardCode);
            try
            {
                DB.ExecuteScalar(dbCommand);
                return true;
            }
            catch
            {
                return false;
            }
           
        }
        public static IDataReader GetByCardCode(string CardCode)
	    {
		    Database DB = DatabaseFactory.CreateDatabase();		
		
            DbCommand dbCommand = DB.GetStoredProcCommand("SAP_AgentList_GetByCardCode", CardCode);
            return DB.ExecuteReader(dbCommand);
	    }
        public static DataTable GetPagination(string whereSql, int PageIndex, int PageSize, string Sort, ref int TotalCount)
	    {
		    Database DB = DatabaseFactory.CreateDatabase();		
		
            DbCommand dbCommand = DB.GetStoredProcCommand("SAP_AgentList_GetPagination");
            DB.AddInParameter(dbCommand, "whereSql", DbType.String,whereSql);
            DB.AddInParameter(dbCommand, "PageIndex", DbType.Int32,PageIndex);
            DB.AddInParameter(dbCommand, "PageSize", DbType.Int32,PageSize);
            DB.AddInParameter(dbCommand, "Sort", DbType.String,Sort);
            DB.AddOutParameter(dbCommand, "TotalCount", DbType.Int32,8);
			DataTable dt=  DB.ExecuteDataSet(dbCommand).Tables[0];
				
            TotalCount=Convert.ToInt32( DB.GetParameterValue(dbCommand,"TotalCount"));
            return dt;
	    }
        public static bool Update(SAP_AgentListInfo obj)
        {
            Database DB = DatabaseFactory.CreateDatabase();

            DbCommand dbCommand = DB.GetStoredProcCommand("SAP_AgentList_Update", obj.CardCode, obj.CardName, obj.AliasName, obj.Address, obj.FromSap, obj.IsShow, obj.LinkeName, obj.Tel1,obj.Banks,obj.Account,obj.VatIdUnCmp);
            try
            {
                DB.ExecuteScalar(dbCommand);
                return true;
            }
            catch (Exception exc)
            {
                return false;
            }
        }
        /// <summary>
        /// 更新配件接收人信息
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static bool PJUpdate(SAP_AgentListInfo obj)
        {
            Database DB = DatabaseFactory.CreateDatabase();

            DbCommand dbCommand = DB.GetStoredProcCommand("SAP_AgentList_PJUpdate", obj.CardCode, obj.PJAcceptName, obj.PJAcceptMobile);
            try
            {
                DB.ExecuteScalar(dbCommand);
                return true;
            }
            catch (Exception exc)
            {
                return false;
            }
        }
    }
}
